package com.karaku.karaku_collector.view;

import android.content.Context;
import android.graphics.Bitmap;
import android.util.AttributeSet;
import android.view.View;
import android.widget.Button;
import android.widget.FrameLayout;
import android.widget.SeekBar;
import android.widget.TextView;
import android.widget.Toast;

import com.karaku.karaku_collector.LocalSetting;
import com.karaku.karaku_collector.R;
import com.karaku.karaku_collector.collector.CollectorManager;
import com.karaku.karaku_collector.common.binder.BindView;
import com.karaku.karaku_collector.common.binder.OnClick;
import com.karaku.karaku_collector.common.binder.UIBinder;
import com.karaku.karaku_collector.util.WhiteBalance;
import com.karaku.karaku_collector.util.actions.Action;
import com.karaku.karaku_collector.util.actions.Action1;
import com.karaku.karaku_collector.util.screenshot.ScreenShotManager;

/**
 * Created by guoping on 2017/11/9.
 */

@BindView(R.layout.view_filter_editor)
public class FilterEditorView extends FrameLayout {

    @BindView(R.id.btn_white_balance)
    private Button btnWhiteBalance;
    @BindView(R.id.tv_desc)
    private TextView tvDesc;
    @BindView(R.id.tv_light)
    private TextView tvLight;
    @BindView(R.id.sb_light)
    private SeekBar sbLight;
    @BindView(R.id.view_content)
    private View contentView;

    private boolean whiteBalance = false;
    private Action1<WhiteBalance.WhiteBalanceResult> balanceCallback;
    private Action1<Float> lightCallback;

    public FilterEditorView(Context context) {
        this(context, null);
    }

    public FilterEditorView(Context context, AttributeSet attributeSet) {
        super(context, attributeSet);
        UIBinder.bind(this, this, true);

        sbLight.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() {
            @Override
            public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {

            }

            @Override
            public void onStartTrackingTouch(SeekBar seekBar) {

            }

            @Override
            public void onStopTrackingTouch(SeekBar seekBar) {

            }
        });

        setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                hide();
            }
        });
    }

    public void show(Action1<WhiteBalance.WhiteBalanceResult> balanceCallback, Action1<Float> lightCallback) {
        this.balanceCallback = balanceCallback;
        this.lightCallback = lightCallback;
        setVisibility(VISIBLE);
        whiteBalance = true;
        updateViewWithWhiteBalanceState();
    }

    public void hide() {
        this.balanceCallback = null;
        this.lightCallback = null;
        CollectorManager.getInstance().setWhiteBalance(LocalSetting.getInstance().getWhiteBalance());
        setVisibility(GONE);
    }

    public void setContentLayoutParams(LayoutParams params) {
        contentView.setLayoutParams(params);
    }

    @OnClick(R.id.btn_white_balance)
    private void onWhiteBalanceClick() {
        if (whiteBalance) {
            ScreenShotManager.start(new Action1<Bitmap>() {
                @Override
                public void run(Bitmap bmp) {
                    WhiteBalance.WhiteBalanceResult r = WhiteBalance.getWhiteBalanceResult(bmp);
//                    Toast.makeText(getContext(), String.format("r:%.2f  g:%.2f  b:%.2f", r.r, r.g, r.b), Toast.LENGTH_LONG).show();
                    whiteBalance = !whiteBalance;
                    updateViewWithWhiteBalanceState();
                    float[] wb = new float[] { 1 / r.r, 1 / r.g, 1 / r.b, 1 };
                    LocalSetting.getInstance().setWhiteBalance(wb);
                    CollectorManager.getInstance().setWhiteBalance(wb);
                    Toast.makeText(getContext(), "调整完成", Toast.LENGTH_SHORT).show();
                    hide();
                }
            });
        } else {
            whiteBalance = !whiteBalance;
            updateViewWithWhiteBalanceState();
        }
    }

    private void updateViewWithWhiteBalanceState() {
        if (whiteBalance) {
            CollectorManager.getInstance().setWhiteBalance(new float[] { 1, 1, 1, 1});
            btnWhiteBalance.setText("完成");
            tvDesc.setVisibility(VISIBLE);
        } else {
            btnWhiteBalance.setText("调整白平衡");
            tvDesc.setVisibility(GONE);
        }
    }
}
